package com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.behavior;


import com.google.common.base.Strings;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ActivitiConstants;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ApplicationContextUtils;
import com.kingyun.gpsinspection.purificationservice.common.enums.AssignTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.activiti.BpmConfNodeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.activiti.BpmConfNode;
import com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.personrule.PersonGroupRule;
import com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.personrule.PersonRule;
import com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.personrule.RoleRule;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.activiti.engine.impl.task.TaskDefinition;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
 * 重写用户任务行为类
 * @author zjh
 * @since 20170601
 */
public class CustomerUserTaskActivityBehavior extends UserTaskActivityBehavior {



    public CustomerUserTaskActivityBehavior(String userTaskId, TaskDefinition taskDefinition) {
        super(userTaskId, taskDefinition);
    }

    @Override
    public void execute(ActivityExecution execution) throws Exception {
        super.execute(execution);
    }


    /**
     * 重写人员配置，实现多种人员配置
     * @param assigneeExpression
     * @param ownerExpression
     * @param candidateUserExpressions
     * @param candidateGroupExpressions
     * @param task
     * @param execution
     */
    @Override
    protected void handleAssignments(Expression assigneeExpression, Expression ownerExpression, Set<Expression> candidateUserExpressions, Set<Expression> candidateGroupExpressions, TaskEntity task, ActivityExecution execution) {
        BpmConfNodeMapper bpmConfNodeMapper =(BpmConfNodeMapper) ApplicationContextUtils.getBean(BpmConfNodeMapper.class);
        PvmActivity activity = execution.getActivity();
        String multiInstance = (String)activity.getProperty(ActivitiConstants.MULTI_PROPERTY);
        if(Strings.isNullOrEmpty(multiInstance)){ //非会签节点
            BpmConfNode bpmConfUser =null;
            List<String> manualPersonList=(List<String>)task.getVariable(ActivitiConstants.MANUAL_PERSON);
            //手动赋值处理人
            if(!CollectionUtils.isEmpty(manualPersonList)){
                task.addCandidateUsers(manualPersonList);

            }else if((bpmConfUser = bpmConfNodeMapper.getBpmConfNodeByNodeIdAndPid(task.getTaskDefinitionKey(), task.getProcessDefinitionId())) != null){ //手动办理
               //手动处理
                handleSingle(task,bpmConfUser,ActivitiConstants.USER_TASK);

            }else { //任务自动处理
                handleSingle(task,bpmConfUser,ActivitiConstants.AUTO_TASK);
            }
        }
        //移除变量
        task.removeVariable(ActivitiConstants.MANUAL_PERSON);
        super.handleAssignments(assigneeExpression, ownerExpression, candidateUserExpressions, candidateGroupExpressions, task, execution);

    }




    /**
     * 处理一般任务节点
     * @param task
     * @param bpmConfUser
     * @param type
     */
    private void handleSingle(TaskEntity task,BpmConfNode bpmConfUser,String type){
        if(ActivitiConstants.AUTO_TASK.equals(type)){
            //自动完成
            task.setAssignee(ActivitiConstants.AUTO_COMPLETE_ASSIGN);
        }else{
            //流程平台配置
            //一般任务
            if(AssignTypeEnum.PERSON.getCode()==bpmConfUser.getUserType()){  //人员配置
                PersonRule.getInstance().assigneRule(null,task,bpmConfUser);
            }else if(AssignTypeEnum.ROLE.getCode()==bpmConfUser.getUserType()){//角色
                RoleRule.getInstance().assigneRule(null,task,bpmConfUser);
            }else if(AssignTypeEnum.USERGROUP.getCode()==bpmConfUser.getUserType()){//用户组
                PersonGroupRule.getInstance().assigneRule(null,task,bpmConfUser);
            }else if(AssignTypeEnum.POSITION.getCode()==bpmConfUser.getUserType()){//岗位
                throw new UnsupportedOperationException("暂不支持岗位配置类型");
            }else{
                throw new UnsupportedOperationException("不支持的配置办理人类型");
            }
        }
    }
}
